<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Backend;
use think\facade\Db;
use think\facade\View;

class Attachment extends Backend
{

    protected function _upload_init($upload)
    {
        $file_type = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
        $ext_arr = array(
            'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
            'flash' => array('swf', 'flv'),
            'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
            'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
        );
        $allow_exts_conf = explode(',', C('fours_attr_allow_exts')); // 读取配置
        $allow_max = C('fours_attr_allow_size'); // 读取配置
        //和总配置取交集
        $allow_exts = array_intersect($ext_arr[$file_type], $allow_exts_conf);
        $allow_max && $upload->maxSize = $allow_max * 1024;   //文件大小限制
        $allow_exts && $upload->allowExts = $allow_exts;  //文件类型限制
        $upload->savePath = './data/upload/editer/' . $file_type . '/';
        $upload->saveRule = 'uniqid';
        $upload->autoSub = true;
        $upload->subType = 'date';
        $upload->dateFormat = 'Y/m/d/';
        return $upload;
    }

    /**
     * 编辑器上传
     */
    public function editer_upload()
    {

        $file_type = empty(request()->param('dir')) ? 'image' : trim(request()->param('dir'));
        $file = request()->file('file');  //这里‘file’是你提交时的name
        try {
            validate(['goodFile' => [     //goodFile是你自定义的键名，目的是为了对check里数组中的
                'fileSize' => 50 * 1024 * 1024,//goodFile字段值进行验证;允许文件大小
                'fileExt' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),  //允许的文件后缀
            ]])->check(['goodFile' => $file]);//也就是对上传的$file进行验证
            $saveName = \think\facade\Filesystem::disk('public')->putFile($file_type, $file,
                function () use ($file) {
                    switch ('date') {
                        case 'date':
                            $fileName = date('YmdHis') . rand(1, 99);
                            break;
                        // 此项即为：保留原始文件名+日期方式
                        case 'original':
                            $fileName = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . date('YmdHis'));
                            break;
                        default:
                            $fileName = uniqid();
                            break;
                    }
                    return $fileName;
                });//保存文件名
            $newname = str_replace($file_type, '', $saveName);
            $url = request()->domain() . '/storage/' . $file_type . '/' . $newname;
            echo '{"code":0,"msg":"成功上传","data":{"src":"' . $url . '"}}';
        } catch (\Exception $e) {
            echo json_encode(array('error' => 1, 'message' => '上传失败!' . $e->getMessage()));
        }
        exit;

    }


}